package cn.tedu.oa_system.service.impl;

import cn.tedu.oa_system.base.json_result.ResultDataDto;
import cn.tedu.oa_system.base.json_result.ResultDto;
import cn.tedu.oa_system.mapper.TypeListMapper;
import cn.tedu.oa_system.pojo.TypeList;
import cn.tedu.oa_system.pojo.in.FindLikeKeyIn;
import cn.tedu.oa_system.pojo.in.TypeListIn;
import cn.tedu.oa_system.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

import static cn.tedu.oa_system.base.constant.DataBaseConstants.DATABASE_AFFECTED_ROW;

/**
 * @Author MW
 * @Description 类型服务实现接口
 * @CreateDate 2023/3/7
 */
@Slf4j
@Service
@Transactional
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeListMapper typeListMapper;

    @Override
    public ResultDto<Void> save(TypeListIn typeListIn) {
        TypeList typeList=new TypeList();
        BeanUtils.copyProperties(typeListIn,typeList);
        int row = typeListMapper.insertType(typeList);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"添加类型失败");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        resultDto.setMes("添加类型成功");
        return resultDto;
    }

    @Override
    public ResultDto<Void> deleteById(TypeListIn typeListIn) {
        Integer id = typeListIn.getId();
        int row = typeListMapper.deleteById(id);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"删除类型失败");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        resultDto.setMes("删除类型成功");
        return resultDto;
    }

    @Override
    public ResultDto<Void> update(TypeListIn typeListIn) {
        TypeList typeList=new TypeList();
        BeanUtils.copyProperties(typeListIn,typeList);
        int row = typeListMapper.updateById(typeList);
        Assert.isTrue(row==DATABASE_AFFECTED_ROW,"修改类型失败");
        ResultDto<Void> resultDto=new ResultDto<>();
        resultDto.setIsSuccess(true);
        resultDto.setMes("修改类型成功");
        return resultDto;
    }

    @Override
    public ResultDataDto<TypeList> list(FindLikeKeyIn likeKeyIn) {
        List<TypeList> typeLists = typeListMapper.listAndLike(likeKeyIn);
        ResultDataDto<TypeList> resultDataDto=new ResultDataDto<>();
        resultDataDto.setIsSuccess(true);
        resultDataDto.setDataList(typeLists);
        return resultDataDto;
    }
}
